package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: Miyako
 * @Date: 2024-08-30-9:39
 * @Description:
 */
//业务层添加Service注解，方便spring进行依赖注入
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    /**
     * @param: 存有用户名和密码的dto类
     * @return: 用户信息 || 抛出异常
     * @description: 登录信息逻辑处理
     **/
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("userlog:"+userLoginParam);
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserVO userVO = userMapper.selectByUsername(username);
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(password)){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }



    /**
     * @param: 从controller类中转收DTO类
     * @return: void
     * @description:处理更新或新增用户的业务逻辑
     **/
    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam, user);
        if (user.getId()==null){
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }

    }

    @Override
    public List<UserQuery> selectUser(UserQuery userQuery) {
        UserVO user = new UserVO();

        BeanUtils.copyProperties(userQuery , user);

        log.debug("传参后"+user);

        List<UserQuery> userQueries = userMapper.selectUser(user);
        log.debug("查询到数据"+userQueries);
        return userQueries;
    }



    @Override
    public void resetPassword(Long userId) {
        log.debug("id是"+userId);
        User user = new User();
        user.setPassword("root");
        user.setId(userId);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }


    @Override
    public void changeStatus(Long userId, String status) {
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
        log.debug("成功更改"+user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug(userId+"删除中...");
        userMapper.deleteUser(userId);
    }
}
